package cn.tedu._07springaop.egaop2;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class CheckAspect {
//    @Before(value = "execution(public boolean cn.tedu._07springaop.egaop2.CartServiceImpl.*(..))")
//    public void before(JoinPoint joinPoint){
//        System.out.println("[前置通知]:开始添加购物车");
//    }
//    @After(value = "execution(public boolean cn.tedu._07springaop.egaop2.CartServiceImpl.*(..))")
//    public void after(JoinPoint joinPoint){
//        System.out.println("[后置通知]:操作完成");
//    }
//    @AfterReturning(value = "execution(public boolean cn.tedu._07springaop.egaop2.CartServiceImpl.*(..))",returning = "result")
//    public void afterReturning(JoinPoint joinPoint,Object result){
//        if (result.equals(true)) {
//            System.out.println("[返回通知]:添加购物车成功");
//        }else {
//            System.out.println("[返回通知]:添加购物车失败");
//        }
//    }
    @Around(value = "execution(public boolean cn.tedu._07springaop.egaop2.CartServiceImpl.*(..))")
    public Object around(ProceedingJoinPoint joinPoint){
        Object result = null;
        try {
            //前置通知
            System.out.println("[环绕前置通知]:开始添加购物车");
            result=joinPoint.proceed();
            //返回通知
            if (result.equals(true)) {
                System.out.println("[环绕返回通知]:添加购物车成功");
            }else {
                System.out.println("[环绕返回通知]:添加购物车失败");
            }
        }catch (Throwable e){
            System.out.println("[环绕异常通知]:出现异常");
            throw new  RuntimeException(e);
            //异常通知
        }finally {
            //后置通知
            System.out.println("[环绕后置通知]:操作完成");
        }
        return result;
    }
}
